package cn.geoportal.benoserver.controller;

import cn.geoportal.benoserver.entity.Spatialdb;
import cn.geoportal.benoserver.entity.common.PageResult;
import cn.geoportal.benoserver.entity.common.RespBean;
import cn.geoportal.benoserver.service.SpatialdbService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ProjectName: gmo
 * @Package: cn.geoportal.benoserver.controller
 * @ClassName: SpatialdbController
 * @Author: chiangbt@geoportal.cn
 * @Description: SpatialDB路由
 * @Date: 2021/9/30 23:08
 */
@Api(tags = "8.参数-空间库设置")
@ApiSupport(order = 501)
@RestController
@RequestMapping("/api/spatialdb")
public class SpatialdbController {
    @Autowired
    private SpatialdbService spatialdbService;

    /**
     * 1. 空间库列表
     * @param pageNo
     * @param pageSize
     * @param query
     * @return
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "SpatialDB列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNo", value = "当前页号", required = true, example = "1"),
            @ApiImplicitParam(name="pageSize", value = "批次数量", required = true, example = "15"),
            @ApiImplicitParam(name = "query",value = "名称", required = false, example = "")
    })
    @RequestMapping(value = "", method = RequestMethod.GET)
    public RespBean list(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize,
                                @RequestParam(value="query", defaultValue = "") String query){
        QueryWrapper<Spatialdb> wrapper = new QueryWrapper<>();
        wrapper.like("name", query.toLowerCase()).or().like("name", query.toUpperCase());
        wrapper.orderByDesc("id");
        // 翻页对象
        Page<Spatialdb> ipage = new Page<>(pageNo, pageSize);
        try{
            IPage<Spatialdb> dbs = spatialdbService.selectPage(ipage, wrapper);
            PageResult<?> pageResult = new PageResult<Spatialdb>(
                    dbs.getCurrent(),
                    dbs.getSize(),
                    dbs.getTotal(),
                    dbs.getRecords());

            return RespBean.success("获取成功", pageResult);
        }catch (Exception e){
            return RespBean.error("无数据");
        }
    }

    /**
     * 2. 新建SDB
     * @param spatialdb
     * @return
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "新建SpatialDB")
    @PostMapping(value = "", consumes = "application/json", produces = "application/json")
    public RespBean add(@RequestBody Spatialdb spatialdb){
        try{
            int result = spatialdbService.addSpatialDb(spatialdb);
            if(0 == result){
                return RespBean.error("新增的记录与库中已有记录的name值重复");
            }
            return RespBean.success("记录添加成功", result);
        }catch (Exception exp){
            return RespBean.error("记录添加不成功");
        }
    }


    /**
     * 3. 获取SDB
     * @param id
     * @return
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "获取指定id的SpatialDB")
    @GetMapping(value = "/{id}", produces = "application/json")
    public RespBean getById(@PathVariable(value="id") Integer id){
        try{
            Spatialdb spatialdb = spatialdbService.findSpatialDbById(id);
            if(null == spatialdb){
                return RespBean.error("指定id的记录不存在");
            }
            return RespBean.success( "记录获取成功", spatialdb);
        }catch (Exception exp){
            return RespBean.error("记录获取不成功");
        }
    }


    /**
     * 4. 更新指定id的SDB
     * @param spatialdb
     * @param id
     * @return
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "更新指定id的SpatialDB")
    @PatchMapping(value = "/{id}", produces = "application/json")
    public RespBean update(@PathVariable(value="id") Integer id, @RequestBody Spatialdb spatialdb){
        try{
            spatialdb.setId(id);
            int result = spatialdbService.updateSpatialDb(spatialdb);
            if(0 == result){
                return RespBean.error("指定更新的记录不存在");
            }
            if(-1 == result){
                return RespBean.error("指定更新的记录与已有记录的name值重复");
            }
            return RespBean.success("记录更新成功", result);
        }catch (Exception exp){
            return RespBean.error("记录更新不成功");
        }
    }

    /**
     * 5. 删除指定id的SDB
     * @param id
     * @return
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "删除指定id的SpatialDB")
    @DeleteMapping(value = "/{id}", produces = "application/json")
    public RespBean delete(@PathVariable(value="id") Integer id){
        try{
            int result = spatialdbService.deleteSpatialDb(id);
            if(0 == result){
                return RespBean.error("指定删除的记录不存在");
            }
            return RespBean.success("记录删除成功", result);
        }catch (Exception exp){
            return RespBean.error("记录删除不成功，错误为"+ exp.getMessage());
        }
    }
}
